<?php
/**
 * Created by PhpStorm.
 * User: 10037
 * Date: 2022/6/1
 * Time: 19:12
 */

namespace app\admin\controller;


use app\admin\model\Foodclass;
use app\admin\model\Foods;
use app\admin\model\Speci;
use think\facade\Request;

class Foodss
{
    public function addFooClass(){
        $data=Request::post();
        $mode=new Foodclass();
        $result= $mode->addFoodClass($data);
        if($result){
            return json([
                'status'=>200
            ]);
        }

        return json([
            'status'=>400
        ]);
    }

    public function selectFooClass(){
        $mode=new Foodclass();
        return $mode->selectFooClass();
    }

    public function addFoods(){
        $data=Request::post();

        $speciList= $data['speciList'];

        unset($data['speciList']);

        $mode=new Foods();
        $fooId= $mode->addFoods($data);

        if(empty($fooId)){
            return json([
                'status'=>'400'
            ]);
        }

        $this->addSpeci($speciList,$fooId);

        return json([
            'status'=>'200'
        ]);

    }

    public function addSpeci($data,$fooId){


        foreach ($data as $val){
            $mode=new Speci();
            $val['foodId']=$fooId;
            $mode->addSpeci($val);
        }
    }

    public function selectFoods(){
        $page=Request::post('page');
        $mode=new Foods();
        $result= $mode->selectFoods($page);
        $count=$this->selectFoodsCount();
        return json([
            'foodsList'=>$result,
            'count'=>$count
        ]);
    }


    public function selectFoodsCount(){
        $mode=new Foods();
        return $mode->where('delete','=',0)->count();
    }

    public function selectSpeci(){
       $fooId=Request::post('foodId');
       $mode=new Speci();
       return $mode->selectSpeci($fooId);
    }

    public function deleteSpeci(){
       $id= Request::post('id');
       $mode=new Speci();
       $mode->deleteSpeci($id);
    }

    public function updateFoods(){
        $id=Request::post('id');
        $mode=new Foods();
        $result=$mode->selectIdFoods($id);
        $result['foodName']=Request::post('foodName');
        $result['foodText']=Request::post('foodText');
        if(!empty(Request::post('foodImg'))){
            $result['foodImg']=Request::post('foodImg');
        }
        $result['foodClassId']=Request::post('foodClassId');
        $flag=$result->save();
        if($flag){
            return json([
                'status'=>200
            ]);
        }

        return json([
            'status'=>400
        ]);
    }

    public function deleteFoods(){
        $id=Request::post('id');
        $mode=new Foods();
        $mode->deleteFoods($id);
    }
}